package com.wangmei.controller;

import com.wangmei.model.Categery;
import com.wangmei.model.CategeryExample;
import com.wangmei.model.Product;
import com.wangmei.model.ProductExample;
import com.wangmei.pojo.UserSimplify;
import com.wangmei.service.CategeryService;
import com.wangmei.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import unitTool.Dictionary;
import unitTool.EmptyTool;
import unitTool.JsonRsp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 产品列表
 */
@RestController
@RequestMapping("/categery")
public class CategeryController {
    private final Logger log = LoggerFactory.getLogger(CategeryController.class);

    @Autowired
    private CategeryService categeryService;
    @Autowired
    private ProductService productService;

    /**
     * 树形：新建pojo
     * 获取列表
     * isdel=1要过滤
     *
     * @return 产品类别（树形结构）JsonRsp(categery)
     */
    @RequestMapping(value = "/getCategeryList", method = RequestMethod.GET)
    public JsonRsp categeryList() {
        try {
            CategeryExample ce = new CategeryExample();
            ce.createCriteria().andIsdelNotEqualTo(Dictionary.DEL_FLAG);
//            return JsonRsp.success(CategeryTree.changeToTree(categeryService.selectByExample(ce)));
            return JsonRsp.success(categeryService.selectByExample(ce));
        } catch (Exception e) {
            log.error("系统内部错误", e);
            return JsonRsp.fail(1000, "系统内部错误");
        }
    }

    /**
     * 查询该分类的产品列表
     *
     * @param cid 传入分类id
     * @return 分类产品列表 JsonRsp(product)
     */
    @RequestMapping(value = "/getProductList", method = RequestMethod.POST)
    public JsonRsp productList(@RequestParam(value = "cid") int cid,
                               @RequestParam(value = "pageIndex", defaultValue = Dictionary.PageDirect.PAGE_INDEX) int index,
                               @RequestParam(value = "pageSize", defaultValue = Dictionary.PageDirect.PAGE_SIZE) int size) {

        try {
            ProductExample pe = new ProductExample();
            pe.createCriteria().andCidEqualTo(cid);
            List<Product> products = productService.selectByExample(pe);
//            PageHelper.startPage(index, size);
//            PageInfo pageInfo = new PageInfo<Product>(products);
//            return JsonRsp.success(pageInfo.getList()).setTotal(pageInfo.getTotal());
            return JsonRsp.success(products);
        } catch (Exception e) {
            log.error("系统内部错误", e);
            return JsonRsp.fail(1000, "系统内部错误");
        }

    }

    /**
     * 首页需要：
     * 需要新的pojo 树形：3层
     * 例：
     * 神秘商店：
     * 活动推荐：
     * 马克杯
     *
     * @return 返回列表和其子类的商品
     * JsonRsp(List<Pojo>)
     */
    @RequestMapping("getCateAndPro")
    public JsonRsp getCateAndPro() {

        return null;
    }

    /**
     * 新增Categery
     */
    @RequestMapping(value = "/addNewCategery", method = RequestMethod.POST)
    public JsonRsp addNewCategery(@RequestParam(value = "pid", required = false) Integer pid,
                                  @RequestParam(value = "cname") String cname,
                                  HttpSession session) {

        try {

            System.out.println(cname);
            Categery categery = new Categery();
            categery.setPid(pid == null ? 0 : pid);
            categery.setCname(cname);
            categery.setIsdel(Dictionary.NOT_DEL_FLAG);
//            UserSimplify us = (UserSimplify) session.getAttribute(Dictionary.SessionKey.CURRENT_USER);
//            if (us == null)
//                return JsonRsp.fail(100, "请先登录");
//            categery.setAdduser(us.getUsername());
            categery.setAddtime(new Date(System.currentTimeMillis()));
            return JsonRsp.success(categeryService.insertSelective(categery));
//            return JsonRsp.success(null);
        } catch (Exception e) {
            log.error("系统内部错误", e);
            return JsonRsp.fail(1000, "系统内部错误");
        }
    }


    @RequestMapping(value = "/deleteCategery", method = RequestMethod.GET)
    public JsonRsp deleteCategery(@RequestParam("cid") Integer cid) {
        try {

            if (null == categeryService.selectByPrimaryKey(cid)) {
                return JsonRsp.fail(100, "无此id对应的记录");
            }

            Categery categery = new Categery();
            categery.setCid(cid);
            categery.setIsdel(Dictionary.DEL_FLAG);
            return JsonRsp.success(categeryService.updateByPrimaryKeySelective(categery));
        } catch (Exception e) {
            log.error("系统内部错误", e);
            return JsonRsp.fail(1000, "系统内部错误");
        }
    }

    @RequestMapping(value = "/updateCategeryPid", method = RequestMethod.POST)
    public JsonRsp updateCategeryPid(@RequestParam("cid") Integer cid,
                                     @RequestParam("pid") Integer pid,
                                     @RequestParam(value = "cname", required = false) String cname,
                                     HttpSession session) {

        try {
            Categery categery = new Categery();
            categery.setCid(cid);
            categery.setPid(pid);
            if (!EmptyTool.isEmpty(cname))
                categery.setCname(cname);
//            UserSimplify us = (UserSimplify) session.getAttribute(Dictionary.SessionKey.CURRENT_USER);
//            if (us == null)
//                return JsonRsp.fail(100, "请先登录");
//            categery.setModifyuser(us.getUsername());
            categery.setModifytime(new Date(System.currentTimeMillis()));
            return JsonRsp.success(categeryService.updateByPrimaryKeySelective(categery));
        } catch (Exception e) {
            log.error("系统内部错误", e);
            return JsonRsp.fail(1000, "系统内部错误");
        }
    }

    @RequestMapping(value = "/selectByCid", method = RequestMethod.GET)
    public JsonRsp selectCategeryByCid(@RequestParam("cid") Integer cid) {
        try {
            return JsonRsp.success(categeryService.selectByPrimaryKey(cid).getCname());
        } catch (Exception e) {
            log.error("系统错误", e);
            return JsonRsp.fail(500, "系统错误");
        }
    }

}
